package com.vcaresolution.logistic.controller;

//import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.vcaresolution.logistic.form.ContactForm;
import com.vcaresolution.logistic.to.ContactTO;
import com.vcaresolution.logistic.util.ApplicationConstants;

/**
 * <code>ContactController</code> is used to track all login related actions
 *
 * @author Dharam Gusai
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
public class ContactController extends BaseController{

	@ModelAttribute(value=ApplicationConstants.CONTACTFORM_MODELATTRIBUTE)
	public ContactForm createForm(){
		return new ContactForm();
	}

	private void loadRequiredDetailInModelObject(ModelAndView modelAndView){		
		modelAndView.addObject(ApplicationConstants.CATEGORYLIST_MODELATTRIBUTE, getServiceRegistry().getCodetableService().getCodetable("CNCATEGORY"));
		modelAndView.addObject(ApplicationConstants.COMPANYLIST_MODELATTRIBUTE, getServiceRegistry().getCompanyService().findByAll());
	}
	
	/**
	 * This method is called when the GET request has been made to the ADDContact Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_CONTACT_URL,method = RequestMethod.GET)
	public ModelAndView loadAddContact() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(ApplicationConstants.ADD_CONTACT_VIEW);
		modelAndView.addObject(ApplicationConstants.CONTACTFORM_MODELATTRIBUTE, new ContactForm());
	
		loadRequiredDetailInModelObject(modelAndView);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADDCOntact Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_CONTACT_URL,method = RequestMethod.POST)
	public ModelAndView processAddContact(@Valid ContactForm contactForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			contactForm.setUser(getLoggedInUser(request));
			
			getServiceRegistry().getContactService().add(contactForm);
			
			modelAndView = list();
		}
		else{
			List<FieldError> fieldErrors = result.getFieldErrors();
			System.out.println("===========================================");
			for(FieldError fieldError : fieldErrors){
				System.out.println(" Error "+fieldError.getField()+" Message "+fieldError.getDefaultMessage());
			}
			System.out.println("===========================================");
		}
		
		loadRequiredDetailInModelObject(modelAndView);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ListContact Request
	 */
	@RequestMapping(value=ApplicationConstants.LIST_CONTACT_URL,method = RequestMethod.GET)
	public ModelAndView list() {
		final ModelAndView modelAndView = new ModelAndView();
		
		List<ContactTO> contactList = getServiceRegistry().getContactService().findByAll();
		
		modelAndView.addObject(ApplicationConstants.CONTACTLIST_MODELATTRIBUTE, contactList);
		modelAndView.setViewName(ApplicationConstants.LIST_CONTACT_VIEW);
		
		loadRequiredDetailInModelObject(modelAndView);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the Edit Contact Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_CONTACT_URL,method = RequestMethod.GET)
	public  ModelAndView loadEditContact(@RequestParam(value="contactId") String contactID) {
		ModelAndView modelAndView = new ModelAndView();
			
		Object contactObject = getServiceRegistry().getContactService().findById(Integer.parseInt(contactID));
		
		modelAndView.addObject(ApplicationConstants.CONTACTFORM_MODELATTRIBUTE, contactObject);
		modelAndView.setViewName(ApplicationConstants.EDIT_CONTACT_VIEW);
		
		loadRequiredDetailInModelObject(modelAndView);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the Edit Contact Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_CONTACT_URL,method = RequestMethod.POST)
	public ModelAndView processEditContact(@Valid ContactForm contactForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			contactForm.setUser(getLoggedInUser(request));
			
			getServiceRegistry().getContactService().update(contactForm);
			
			modelAndView = list();
		}
		
		loadRequiredDetailInModelObject(modelAndView);
		
		return modelAndView;
	}
}